﻿@namespace CyssBlazor.Shared.Components
@inherits BaseComponent
<div class="dropdown-item" @onclick="@OnClick" disabled="@Disabled" aria-disabled="@Disabled">
    <i class="@Icon"></i>
    <span>@Text</span>
</div>

@code
    {

    public object Item { set; get; }

    [Parameter]
    public string Text { set; get; }

    /// <summary>
    /// 获得/设置 显示图标
    /// </summary>
    [Parameter]
    public string? Icon { get; set; }

    /// <summary>
    /// 获得/设置 OnClick 事件
    /// </summary>
    [Parameter]
    public EventCallback<MouseEventArgs> OnClick { get; set; }

    /// <summary>
    /// 获得/设置 OnClick 事件不刷新父组件
    /// </summary>
    [Parameter]
    public Func<Task>? OnClickWithoutRender { get; set; }

    /// <summary>
    /// 是否确认提示
    /// </summary>
    [Parameter]
    public bool IsConfirm { set; get; }

    /// <summary>
    /// 确认提交提示消息
    /// </summary>
    [Parameter]
    public string ConfirmTitle { set; get; }

    [Parameter]
    public string LoadingText { set; get; } = "";

    [Parameter]
    public string LoadingIcon { set; get; } = "fa fa-spinner fa-spin fa-fw";

    /// <summary>
    /// 获得/设置 点击确认时回调方法
    /// </summary>
    [Parameter]
    public Func<Task> OnConfirm { get; set; }

    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

        var onClick = OnClick;
        OnClick = EventCallback.Factory.Create<MouseEventArgs>(this, async e =>
        {
            string text = this.Text;
            string icon = this.Icon;
            if (!IsDisabled)
            {
                if (IsConfirm)
                {
                    var isOk = await dialogHelper.ShowConfirm(ConfirmTitle);
                    if (isOk)
                    {
                        if (!string.IsNullOrWhiteSpace(this.LoadingText))
                        {
                            this.Text = this.LoadingText;
                        }
                        this.Icon = this.LoadingIcon;
                        IsDisabled = true;
                        await InvokeAsync(StateHasChanged);
                        await OnConfirm();
                        IsDisabled = false;
                        this.Icon = icon;
                        this.Text = text;
                    }
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(this.LoadingText))
                    {
                        this.Text = this.LoadingText;
                    }
                    this.Icon = this.LoadingIcon;
                    IsDisabled = true;
                    await InvokeAsync(StateHasChanged);
                    if (OnClickWithoutRender != null) await OnClickWithoutRender.Invoke();
                    if (onClick.HasDelegate) await onClick.InvokeAsync(e);
                    IsDisabled = false;
                    this.Icon = icon;
                    this.Text = text;
                }
                await InvokeAsync(StateHasChanged);
            }
        });
    }

    /// <summary>
    /// 获得 按钮 disabled 属性
    /// </summary>
    protected string? Disabled => IsDisabled ? "true" : null;

    /// <summary>
    /// 获得/设置 是否禁用 默认为 false
    /// </summary>
    [Parameter]
    public bool IsDisabled { get; set; }
}
